package net.lab1024.smartadmin.module.business.easyComputeModel.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import net.lab1024.smartadmin.common.domain.PageResultDTO;
import net.lab1024.smartadmin.common.domain.ResponseDTO;
import net.lab1024.smartadmin.module.business.easyComputeModel.dao.EasyComputeModelDao;
import net.lab1024.smartadmin.module.business.easyComputeModel.domain.dto.EasyComputeModelQueryDTO;
import net.lab1024.smartadmin.module.business.easyComputeModel.domain.dto.EasyComputeModelResultAddDTO;
import net.lab1024.smartadmin.module.business.easyComputeModel.domain.dto.EasyComputeModelResultUpdateDTO;
import net.lab1024.smartadmin.module.business.easyComputeModel.domain.entity.EasyComputeModelEntity;
import net.lab1024.smartadmin.module.business.easyComputeModel.domain.vo.EasyComputeModelExcelVO;
import net.lab1024.smartadmin.module.business.easyComputeModel.domain.vo.EasyComputeModelVO;
import net.lab1024.smartadmin.util.SmartBeanUtil;
import net.lab1024.smartadmin.util.SmartPageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * [ �򵥼������ģ�� ]
 *
 * @author �Ζ�
 * @version 1.0
 * @company ��������
 * @copyright (c)  ��������Inc. All rights reserved.
 * @date 2021-11-27 13:58:04
 * @since JDK1.8
 */
@Service
public class EasyComputeModelService {

    @Autowired
    private EasyComputeModelDao easyComputeModelDao;

    /**
     * ����id��ѯ
     */
    public EasyComputeModelEntity getById(Long id){
        return  easyComputeModelDao.selectById(id);
    }

    /**
     * ��ҳ��ѯ
     * @author �Ζ�
     * @date 2021-11-27 13:58:04
     */
    public ResponseDTO<PageResultDTO<EasyComputeModelVO>> queryByPage(EasyComputeModelQueryDTO queryDTO) {
        Page page = SmartPageUtil.convert2QueryPage(queryDTO);
        IPage<EasyComputeModelVO> voList = easyComputeModelDao.queryByPage(page, queryDTO);
        PageResultDTO<EasyComputeModelVO> pageResultDTO = SmartPageUtil.convert2PageResult(voList);
        return ResponseDTO.succData(pageResultDTO);
    }

    /**
     * ���
     * @author �Ζ�
     * @date 2021-11-27 13:58:04
     */
    public Long add(EasyComputeModelResultAddDTO addDTO) {
        EasyComputeModelEntity entity = SmartBeanUtil.copy(addDTO, EasyComputeModelEntity.class);
         easyComputeModelDao.addEasyComputeModel(entity);
        return entity.getId();
    }

    /**
     * �༭
     * @author �Ζ�
     * @date 2021-11-27 13:58:04
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> update(EasyComputeModelResultUpdateDTO updateDTO) {
        EasyComputeModelEntity entity = SmartBeanUtil.copy(updateDTO, EasyComputeModelEntity.class);
        easyComputeModelDao.updateEasyComputeModelByName(entity);
        return ResponseDTO.succ();
    }

    /**
     * ɾ��
     * @author �Ζ�
     * @date 2021-11-27 13:58:04
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> deleteByIds(List<Long> idList) {
        easyComputeModelDao.deleteByIdList(idList);
        return ResponseDTO.succ();
    }

    /**
     * ��ѯȫ����������
     * @author �Ζ�
     * @date 2021-11-27 13:58:04
     */
    public List<EasyComputeModelExcelVO> queryAllExportData(EasyComputeModelQueryDTO queryDTO) {
        QueryWrapper<EasyComputeModelEntity> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(EasyComputeModelEntity::getSn,queryDTO.getName());
        List<EasyComputeModelEntity> model= easyComputeModelDao.selectList(queryWrapper);
        List<EasyComputeModelExcelVO> list= Lists.newArrayList();
        model.stream().filter(e->{list.add(SmartBeanUtil.copy(model,EasyComputeModelExcelVO.class));return true;});
        return list;
    }

    /**
     * ������ѯ��������
     * @author �Ζ�
     * @date 2021-11-27 13:58:04
     */
    public List<EasyComputeModelExcelVO> queryBatchExportData(List<Long> idList) {
        return easyComputeModelDao.queryBatchExportData(idList);
    }

    public Integer queryByName(String name) {
        QueryWrapper<EasyComputeModelEntity> queryWrapper = new QueryWrapper<EasyComputeModelEntity>();
        queryWrapper.eq("name",name);
        return easyComputeModelDao.selectCount(queryWrapper);
    }

    public EasyComputeModelEntity queryOneByName(String name) {
        QueryWrapper<EasyComputeModelEntity> queryWrapper = new QueryWrapper<EasyComputeModelEntity>();
        queryWrapper.eq("name",name);
        return easyComputeModelDao.selectOne(queryWrapper);
    }
}
